#include "Box.h"
#include "FuriousTourist.h"

Box::Box(float width, float height, float depth, ObjectMaterial material)
{
	mHasHealth = true;
	mShowHealth = false;

	body = new physics::RigidBody;
	mHealth = 100;
	mMaterial = material;

	physics::real w = (physics::real)width;
	physics::real h = (physics::real)height;
	physics::real d = (physics::real)depth;

	CollisionBox::halfSize = physics::Vector3(w, h, d);

	physics::real mass = w * h * d * GetDensity(material);
    body->setMass(mass);

    physics::Matrix3 tensor;
    tensor.setBlockInertiaTensor(halfSize, mass);
    body->setInertiaTensor(tensor);

	body->setDamping(0.8, 0.8);
	body->setAwake();
	body->SetMaterial(mMaterial);
	
	// choose the appropriate mesh, depending on the material
	switch(material)
	{
	case OBJECTMATERIAL_STONE:
		mObjectType = OBJECTTYPE_STONEBOX;
		break;

	case OBJECTMATERIAL_WOOD:
		mObjectType = OBJECTTYPE_WOODBOX;
		break;

	case OBJECTMATERIAL_SAND:
		mObjectType = OBJECTTYPE_SANDBOX;
		break;

	case OBJECTMATERIAL_EXPLOSIVE:
		mObjectType = OBJECTTYPE_EXPLOSIVEBOX;
		break;

	default:
		mObjectType = OBJECTTYPE_DEFAULTBOX;
		break;
	}

	D3DXMatrixScaling(&mScale, width, height, depth);

}


Box::~Box(void)
{
	//mVertexBuffer->Release();
	//mIndexBuffer->Release();
	//mTexture->Release();
	delete body;
}

void Box::Render(RenderInfo ri)
{
	BufferData* bufferData = MESHMANAGER->GetBuffer(mObjectType);

	// point the graphics card at the vertex and index buffers
	gD3DDevice->SetVertexDeclaration(VertexPNT::Decl);
	gD3DDevice->SetStreamSource(0, bufferData->vbuffer, 0, sizeof(VertexPNT));
	gD3DDevice->SetIndices(bufferData->ibuffer);

	D3DXMATRIX W, WIT;

	// for debugging, if we want to show asleep/deactivated objects in
	// different colors
	
#ifdef _SHOWSTATECOLORS
	if (!body->getActivated())
	{
		Mtrl mtrl = Mtrl(RED, RED, RED, 1.0f);
		ri.mFX->SetValue(ri.mhAmbientMtrl, &mtrl.ambient, sizeof(D3DXCOLOR));
	}
	else if (!body->getAwake())
	{
		Mtrl mtrl = Mtrl(BLUE, BLUE, BLUE, 1.0f);
		ri.mFX->SetValue(ri.mhAmbientMtrl, &mtrl.ambient, sizeof(D3DXCOLOR));
	}
	else
	{
		ri.mFX->SetValue(ri.mhAmbientMtrl, &mMtrl.ambient, sizeof(D3DXCOLOR));
	}
#else
	ri.mFX->SetValue(ri.mhAmbientMtrl, &bufferData->mtrl->ambient, sizeof(D3DXCOLOR));
#endif

	//ri.mFX->SetValue(ri.mhDiffuseMtrl, &bufferData->mtrl->diffuse, sizeof(D3DXCOLOR));

	if (mShowHealth)
	{
		int r = (100 - mHealth) * 2.5;
		int g = 250 - ((100 - mHealth) * 2.5);
		
		D3DCOLOR color = D3DCOLOR_XRGB(r, g, 0);
		Mtrl mtrl = Mtrl(color, color, color, 1.0f);
		ri.mFX->SetValue(ri.mhAmbientMtrl, &mtrl.ambient, sizeof(D3DXCOLOR));
	}
	else
	{
		ri.mFX->SetValue(ri.mhDiffuseMtrl, &bufferData->mtrl->diffuse, sizeof(D3DXCOLOR));
		ri.mFX->SetValue(ri.mhSpecMtrl, &bufferData->mtrl->spec, sizeof(D3DXCOLOR));
		ri.mFX->SetFloat(ri.mhSpecPower, bufferData->mtrl->specPower);
	}

	

	ri.mFX->SetTexture(ri.mhTex, bufferData->texture);

	W = mScale * body->getDXTransform();
	D3DXMatrixInverse(&WIT, 0, &W);
	D3DXMatrixTranspose(&WIT, &WIT);

	ri.mFX->SetMatrix(ri.mhWVP, &(W * ri.mView * ri.mProj));
	ri.mFX->SetMatrix(ri.mhWorld, &W);
	ri.mFX->SetMatrix(ri.mhWorldInvTrans, &WIT);
	ri.mFX->CommitChanges();
	gD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
	gD3DDevice->SetRenderState(D3DRS_WRAP0, 0);
}